package com.systop.asset.service.impl;

import com.systop.asset.entity.ProcessTemplate;
import com.systop.asset.entity.RestResponse;
import com.systop.asset.enums.HttpCodeEnum;
import com.systop.asset.mapper.ProcessTemplateMapper;
import com.systop.asset.service.IProcessTemplateService;
import com.systop.asset.service.IWorkFlowService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;


/**
 * 审批模板Service业务层处理
 * 
 * @author mu
 * @date 2025-06-24
 */
@Service
public class ProcessTemplateServiceImpl implements IProcessTemplateService {
    @Resource
    private ProcessTemplateMapper processTemplateMapper;
    @Autowired
    private IWorkFlowService workFlowService;
    //文件上传的根路径
    @Value("${upload.path}")
    private String rootPath;
    /**
     * 查询审批模板
     * 
     * @param id 审批模板主键
     * @return 审批模板
     */
    @Override
    public ProcessTemplate selectProcessTemplateById(Long id)
    {
        return processTemplateMapper.selectProcessTemplateById(id);
    }

    /**
     * 查询审批模板列表
     * 
     * @param processTemplate 审批模板
     * @return 审批模板
     */
    @Override
    public List<ProcessTemplate> selectProcessTemplateList(ProcessTemplate processTemplate)
    {
        return processTemplateMapper.selectProcessTemplateList(processTemplate);
    }

    /**
     * 新增审批模板
     * 
     * @param processTemplate 审批模板
     * @return 结果
     */
    @Override
    @Transactional
    public RestResponse insertProcessTemplate(ProcessTemplate processTemplate){
        //创建时间赋值
        processTemplate.setCreateTime(new Date());
        //流程部署
        RestResponse response= workFlowService.deployProcess(processTemplate);
        //新增流程模板
        processTemplateMapper.insertProcessTemplate(processTemplate);

        return response;
    }

    /**
     * 修改审批模板
     * 
     * @param processTemplate 审批模板
     * @return 结果
     */
    @Override
    public int updateProcessTemplate(ProcessTemplate processTemplate)
    {
        processTemplate.setUpdateTime(new Date());
        return processTemplateMapper.updateProcessTemplate(processTemplate);
    }

    /**
     * 批量删除审批模板
     * 
     * @param id 需要删除的审批模板主键
     * @return 结果
     */
    @Override
    public RestResponse deleteProcessTemplateByIds(Long id)
    {
        //根据id查询
        ProcessTemplate template = processTemplateMapper.selectProcessTemplateById(id);
        try {
           //删除流程部署
           workFlowService.deleteProcess(template);
           //删除流程模板
           processTemplateMapper.deleteProcessTemplateById(id);
        }catch (Exception e){
           return RestResponse.failure(HttpCodeEnum.DELETE_TEMPLATE_ERROR);
        }

        return RestResponse.success("删除成功");
    }

    /**
     * 删除审批模板信息
     * 
     * @param id 审批模板主键
     * @return 结果
     */
    @Override
    public int deleteProcessTemplateById(Long id)
    {
        return processTemplateMapper.deleteProcessTemplateById(id);
    }

    @Override
    public InputStream getBpmnFile(Long id, String type) {
        ProcessTemplate template = processTemplateMapper.selectProcessTemplateById(id);
        return workFlowService.selectBpmnFile(template,type);
    }
}
